package com.jeesite.common.ueditor.upload;

import com.jeesite.common.ueditor.define.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class Uploader {

	private HttpServletRequest request = null;
	private Map<String, Object> conf = null;

	@Autowired
	Environment environment;

	public Uploader(HttpServletRequest request, Map<String, Object> conf) {
		this.request = request;
		this.conf = conf;
	}

	public final State doExec() {
		String filedName = (String) this.conf.get("fieldName");
		State state = null;

		if ("true".equals(this.conf.get("isBase64"))) {
			//反射到oss上传
//			state = Base64Uploader.save(this.request, this.request.getParameter(filedName),
//					this.conf);
			try {
				Class<?> OssBase64Uploader = Class.forName("com.jeesite.modules.ueditor.OssBase64Uploader");
				Method saveClass = OssBase64Uploader.getMethod("ossSave", HttpServletRequest.class, String.class, Map.class);
				state = (State) saveClass.invoke(null, this.request, this.request.getParameter(filedName), this.conf);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		} else {
			try {
				Class<?> OssBinaryUploader = Class.forName("com.jeesite.modules.ueditor.OssBinaryUploader");
				Method saveClass = OssBinaryUploader.getMethod("ossSave", HttpServletRequest.class, Map.class);
				state = (State) saveClass.invoke(null, this.request, this.conf);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}

		return state;
	}
}
